package com.windfone.common;

import java.io.File;

public class FileTool {
	public static void dealPic(String prefix ,String suffix){
		String path = prefix + suffix;
		File f = new File(path);
		if ( f.exists())
			f.delete();
	}
	public static void dealPic(String suffix){
		String prefix = WebVariable.getWEB_ROOT_PATH();
		String path = prefix + suffix;
		File f = new File(path);
		if ( f.exists())
			f.delete();
	}
	public static void dealFile(String suffix){
		String prefix = WebVariable.getWEB_ROOT_PATH();
		String path = prefix + suffix;
		File f = new File(path);
		if ( f.exists())
			f.delete();
	}
	public static boolean judgeFolderFileOverNumber(String path) {
		boolean flag = false;
		try {
			File f = new File(path);
			if (f.isDirectory()) {
				if (f.listFiles().length > Constant.PIC_NUMBER_LIMIT)
					flag = true;
			}
		} catch (Exception e) {

		}
		return flag;
	}

 
	/**
	 * file 2048*2048*2048*2048
	 * 
	 * @param path
	 *            path like /tomcat_home/webapps/windfones/upload/promotions/
	 * @return
	 */
	public static String getFolderPath(String path, int level, boolean rtv) {
		/**
		 * 给出一个路径，1 如果该路径是存在，则统计该文件夹下有多少个子文件夹或文件,不存在则生成,1.1如果该路径下的文件夹包含的子文件夹
		 * 或子文件夹不大于2048个，则再往下一级探索(递归探索)，直至文件夹深度为3,1.1.1如果深度为3的文件夹下包含的文件超过
		 * 2048个则退回上一级路径，再重新判断该级路径下的子文件夹是否已满足大于2048个，没有则新增一个文件夹，重新递归
		 * 1.2如果文件夹大于2048则 执行1.1.1
		 */
		// 0  1. upload/,0,false ==> 2. upload/1/,1,false,==> 3. upload/1/1/,2,false==>4 upload/1/1/1/,3,false
		if (level <= 3) {
			try {
				File f = new File(path);
				if (!f.exists())
					f.mkdirs();
				if (judgeFolderFileOverNumber(path) && !rtv && (level==3 )) {
					rtv = true;
					path = getFolderPath(path, level, rtv);//如果是叶子才可以往回处理
 
				} else {
					if (rtv){//处理判断已满的情况,路径后退一级 upload/1/1/1/=> upload/1/1/
						level = level - 1;
						int end = path.lastIndexOf(File.separator) - 1;
						path = path.substring(0, end);
						end = path.lastIndexOf(File.separator) + 1;
						path = path.substring(0, end);
						if ( judgeFolderFileOverNumber( path ) ){
							path = getFolderPath(path, level, rtv);
						} else {
							File f2 = new File(path);
							int fs = f2.listFiles().length;
							
							if (fs ==0 )
								fs = 1;
							else
								fs = fs + 1;//对当前文件夹判断，不大于2048则切换文件夹
							if (path.substring(path.length() - 1, path.length()).equals(File.separator))
								path = path + "" + fs + File.separator ;
							else
								path = path + File.separator + "" + fs +  File.separator ;
							
							level= level + 1;
							if ( level <=3 ) {
								rtv = false;
								path = getFolderPath(path, level, rtv);
							}
						}
					} else {
						int fs = f.listFiles().length;
						if (fs ==0 )
							fs = 1;
						
						if ( level <3 ){
							if (path.substring(path.length() - 1, path.length()).equals(File.separator))
								path = path + "" + fs + File.separator ;
							else
								path = path + File.separator + "" + fs +  File.separator ;
							level = level + 1;
							path = getFolderPath(path, level, rtv);
						}
					}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		} 
		return path;
	}

	public static void main(String args[]){
		String str = "H:/development/apache-tomcat-6.0.26/webapps/windfone/upload/promotions/1/1/1/aaImage1 (7).jpg";
		int begin = str.indexOf("upload");
		System.out.println( str.substring(begin, str.length()));
	}
}
